<?php

class User extends Controller {

	function __construct() {
		parent::__construct();
		//echo "user Controller";
		Session::init();
		$logged = Session::get('loggedIn');
		$role  = Session::get('role');
		if ($logged == false || $role != 'owner') {
			Session::destroy();
			header('location: login');
			exit;	
		}
		
		//print_r($_SESSION);
		//$this->view->js = array('user/js/user.js');
		
	}
	
	function index() {
		$this->view->userList = $this->model->userList();
		$this->view->render('user/index');
	}

	function createUser(){
		$data  = array();
		$data['login'] = $_POST['login'];
		$data['password'] = md5($_POST['password']);
		$data['role'] = $_POST['role'];

		//@todo: do error checking

		$this->model->createUser($data);
		header('location: '. URL . 'user');
	}

	function edit($id){
		
		$this->view->user = $this->model->userSingleList($id);
		$this->view->render('user/edit');

	}

	function editSave($id){
		$data  = array();
		$data ['id'] = $id;
		$data['login'] = $_POST['login'];
		$data['password'] = md5($_POST['password']);
		$data['role'] = $_POST['role'];
		
		$this->model->editSave($data);
		header('location: '. URL .'user');	
	}

	function deleteUser($id){

		$this->model->deleteUser($id);
		header('location: '. URL . 'user');

	}


}